/**
 * 
 */
package com.et114.modules.content.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.components.simpletable.DataList;
import com.et114.components.xmlparse.XMLUtils;
import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.AppContext;
import com.et114.core.common.CONST;
import com.et114.core.utility.NumberUtils;
import com.et114.modules.content.services.CommonContentImpl;
import com.et114.modules.content.vo.CommonType;
import com.et114.modules.products.commons.Const;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

/*
 * @author guanhw
 */
public class CommonTypeAction extends BaseDispatchAction {
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward getCommonType( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {

		//DIC  COMMON_CONTENT_TYPE
		List< BizOption > dicOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_COMMON_CONTENT_TYPE ,  true  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "dicOps" , dicOps ) ;
		
		
		CommonTypeForm typeForm = ( CommonTypeForm ) form ; 
		CommonType commonType = new CommonType( ) ; 
		
		Byte globalKey = typeForm.getGlobalKey ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( globalKey ) ) { 
			throw new Exception ( "globalKey is NULL !" ) ; 
		}
		
		PropertyUtils.copyProperties ( commonType , typeForm ) ; 
		Long typeId = commonType.getTypeId ( ) ; 
		
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		if ( ! NumberUtils.isEmptyOrZero ( typeId ) ) { // load one
			commonType = cci.getCommonTypeById ( typeId ) ; 
			request.setAttribute ( "vo" , commonType  ) ;
			return mapping.findForward ( "commonType" ) ;
		}
		else {
			DataList dl = cci.getCommonTypes (  commonType ) ; 
			request.setAttribute ( "searchvo" , commonType ) ; 
			request.setAttribute ( "dl" , dl ) ; 
			if ( request.getParameter ( "flg" ) != null )
				request.setAttribute ( "flg" , request.getParameter ( "flg" ) );
			return mapping.findForward ( "commonTypes" ) ;
		} 
	}
	
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward toEditCommonType( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC  COMMON_CONTENT_TYPE
		List< BizOption > dicOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_COMMON_CONTENT_TYPE ,  true  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "dicOps" , dicOps ) ;

		CommonTypeForm typeForm = ( CommonTypeForm ) form ; 
		CommonType commonType = new CommonType( ) ; 
		
		PropertyUtils.copyProperties ( commonType , typeForm ) ; 

		Byte globalKey = typeForm.getGlobalKey ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( globalKey ) ) { 
			throw new Exception ( "globalKey is NULL !" ) ; 
		}		

		request.setAttribute ( "vo" , commonType  ) ;
		return mapping.findForward ( "commonType" ) ; 
	}
	
	public  ActionForward editCommonType( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {

		CommonTypeForm typeForm = ( CommonTypeForm ) form ; 
		CommonType commonType = new CommonType( ) ; 
		
		Byte globalKey = typeForm.getGlobalKey ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( globalKey ) ) { 
			throw new Exception ( "globalKey is NULL !" ) ; 
		}
		
		commonType.setUpdtUser ( getCurrentUser ( request ).getUserId ( ) ) ;
		
		PropertyUtils.copyProperties ( commonType , typeForm ) ;
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		Long typeId = commonType.getTypeId ( ) ; 
		if ( ! NumberUtils.isEmptyOrZero ( typeId ) ) { // update
			cci.updateCommonType ( commonType ) ; 
		}
		else { //new 
			typeId = cci.saveCommonType ( commonType ) ; 
		}
		
		
		return new ActionForward ( "ct.go?method=getCommonType&flg=1&globalKey="
				+ globalKey + "&typeId=" + typeId + "&parentId=" + commonType.getParentId ( ), true );
	}
	
	
	public  ActionForward removeCommonType( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {

		CommonTypeForm typeForm = ( CommonTypeForm ) form ; 
		CommonType commonType = new CommonType( ) ; 
		
		PropertyUtils.copyProperties ( commonType , typeForm ) ;
		
		Byte globalKey = typeForm.getGlobalKey ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( globalKey ) ) { 
			throw new Exception ( "globalKey is NULL !" ) ; 
		}
		
		
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		
		Long[] deleteIds = typeForm.getDeleteId ( ) ; 
		if ( deleteIds != null ) {
			for ( Long deleteId : deleteIds ) {
				cci.removeCommonType ( deleteId  ) ; 
			}
		}

		
		return new ActionForward ( "ct.go?method=getCommonType&flg=1&globalKey="
				+ globalKey + "&parentId=" + commonType.getParentId ( ) , true );
	}
	
	
	public  ActionForward toCommonTypeIndex( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {

		CommonTypeForm typeForm = ( CommonTypeForm ) form ; 
		CommonType commonType = new CommonType( ) ; 
		
		PropertyUtils.copyProperties ( commonType , typeForm ) ;
		
		Byte globalKey = typeForm.getGlobalKey ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( globalKey ) ) { 
			throw new Exception ( "globalKey is NULL !" ) ; 
		}
		
		
		request.setAttribute ( "vo" , commonType  ) ;
		return mapping.findForward ( "commonContentIndex" ) ;

	}
	
	public  ActionForward contentManager( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {

		CommonTypeForm typeForm = ( CommonTypeForm ) form ; 
		CommonType commonType = new CommonType( ) ; 
		
		PropertyUtils.copyProperties ( commonType , typeForm ) ;
		
		Byte globalKey = typeForm.getGlobalKey ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( globalKey ) ) { 
			throw new Exception ( "globalKey is NULL !" ) ; 
		}
		
		
		request.setAttribute ( "vo" , commonType  ) ;

		return mapping.findForward ( "contentManager" ) ; 
	}
	
	public  ActionForward generateCommonTypeTree( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		response.setContentType ( "text/xml;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );
		
		CommonTypeForm typeForm = ( CommonTypeForm ) form ; 
		CommonType commonType = new CommonType( ) ; 
		
		PropertyUtils.copyProperties ( commonType , typeForm ) ;
		
		Byte globalKey = typeForm.getGlobalKey ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( globalKey ) ) { 
			throw new Exception ( "globalKey is NULL !" ) ; 
		}
		
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		List< CommonType > rootCommonTypes= cci.getRootCommonTypes ( commonType ) ;
		
		StringBuffer sb = new StringBuffer ( );
		sb.append("<?xml version=\"1.0\" encoding=\"" + AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) + "\"?>");
		sb.append ( "<tree id=\"0\"><item text=\"Type Manager\" id=\"-1\" open=\"1\" >" )
				.append ( "\n" );
		if ( rootCommonTypes != null )
			for ( CommonType root : rootCommonTypes )  {
				String tn = root.getTypeCn ( ) ; 
				if ( tn != null )
					tn = XMLUtils.writeEscapedXml( tn.toCharArray ( ) ) ; 
				sb.append ( "<item text=\"" + tn + "\" id=\""
								+ root.getTypeId ( ) + "\" />" ).append ( "\n" );
		
			}
		sb.append ( "</item></tree> " );
		
	
		response.getWriter ( ).print (  sb.toString ( ) );
		response.getWriter ( ).flush ( );
		response.getWriter ( ).close ( );
		return null;
		
	
	}
	
}
